package springcloud.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buba.springcloud.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import springcloud.mapper.DataMapper;
import springcloud.service.IChinaDataService;
import springcloud.service.ICityDataService;
import springcloud.service.IDataService;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
public class DataController {
    //注入服务发现的注解
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private IDataService dataService;

    @Autowired
    private IChinaDataService chinaDataService;

    @Autowired
    private ICityDataService cityDataService;



    @Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "EpidemicData/consul")
    public String paymentConsul() {
        return "SpringCloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString();
    }

    @CrossOrigin(methods = RequestMethod.GET)
    @GetMapping("/EpidemicData/get/{name}")
    public CommonResult queryById(@PathVariable("name") String name){
//        ProvinceData provinceData = dataMapper.selectById("715");
        ProvinceData provinceData = dataService.selectByName(name);
        log.info("***************查询成功*********"+provinceData);
        if(provinceData!=null){
            return new CommonResult(200,"查询成功",provinceData);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

    @CrossOrigin(methods = RequestMethod.GET)
    @GetMapping("/EpidemicData/getChinaData")
    public CommonResult queryById(){
        ChinaData chinaData = chinaDataService.getChinaData();
        log.info("***************查询成功*********"+chinaData);
        if(chinaData!=null){
            return new CommonResult(200,"查询成功",chinaData);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

    @CrossOrigin(methods = RequestMethod.GET)
    @GetMapping("/EpidemicData/getCityConfirmData")
    public CommonResult getCityConfirmData(){
        ArrayList<CityData> cityData = cityDataService.getCityData();
        log.info("***************查询成功*********"+cityData);
        if(cityData!=null){
            return new CommonResult(200,"查询成功",cityData);
        }else{
            return new CommonResult(444,"查询失败",null);
        }
    }

//    @CrossOrigin(methods = RequestMethod.GET)
//    @GetMapping("/EpidemicData/get/{id}")
//    public CommonResult queryById(@PathVariable("id") Long id){
//        MaterialApplication materialApplication = materialApplicationMapper.selectById(id);
//        log.info("***************查询成功*********"+materialApplication);
//        if(materialApplication!=null){
//            return new CommonResult(200,"查询成功",materialApplication);
//        }else{
//            return new CommonResult(444,"查询失败",null);
//        }
//    }

//    @CrossOrigin(methods = RequestMethod.POST)
//    @PostMapping("/MaterialApplication/save")
//    public CommonResult saveMaterialApplication(@RequestBody(required=false) MaterialApplication materialApplication){
//        log.info("***************开始保存*********");
//        int result = this.materialApplicationMapper.insert(materialApplication);
//        log.info("***************保存申请成功*********"+result);
//        if(result!=0){
//            return new CommonResult(200,"保存成功",materialApplication);
//        }else{
//            return new CommonResult(444,"保存失败",null);
//        }
//    }

    //获取服务信息
    @GetMapping("/EpidemicData/discovery")
    public  Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String s : services){
            log.info("********注册到eureka中的服务中有:"+services);
        }
        List <ServiceInstance> instances = discoveryClient.getInstances("MCROSERVICE-PAYMENT");
        for (ServiceInstance s: instances) {
            log.info("当前服务的实例有"+s.getServiceId()+"\t"+s.getHost()+"\t"+s.getPort()+"\t"+s.getUri());
        }
        return this.discoveryClient;
    }
}
